package vn.sms.lazylove.database;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import vn.sms.lazylove.object.OneSMS;
import vn.sms.lazylove.object.SchedulerSMSItem;
import vn.sms.lazylove.object.SendSms;
import vn.sms.lazylove.object.Sms;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;

public class DataStore {
	private static final String LOG_TAG = DataStore.class.getSimpleName();
	protected static DataStore _sharedInstance = null;
	protected static Object _syncObject = new Object();
	protected static Object _staticObject = new Object();

	private Context mContext;

	private static DatabaseManager mDbManager;

	public static DataStore getInstance() {
		synchronized (_staticObject) {
			if (_sharedInstance == null) {
				// doKilledErrorNotify();
			}
			return _sharedInstance;
		}
	}

	public static void init(Context context) {
		synchronized (_staticObject) {
			if (_sharedInstance == null) {
				_sharedInstance = new DataStore(context.getApplicationContext());
				_sharedInstance.initInternal();
			}
		}
	}

	public static void reinit(Context context) {
		synchronized (_staticObject) {
			if (_sharedInstance == null) {
				_sharedInstance = new DataStore(context.getApplicationContext());
				_sharedInstance.initInternal();
				Log.d("SEOOTOOOOOOOOOOOOO", "DDDDDDDDDDDDDDDDDDDDDDDDDDD");
			}
		}
	}

	public static void uninit() {
		synchronized (_staticObject) {
			if (_sharedInstance != null) {
				_sharedInstance.release();
				_sharedInstance = null;
			}
		}
	}

	/**
	 * Hiding constructor
	 */
	private DataStore(Context context) {
		mContext = context;
		mDbManager = DatabaseManager.getWritableInstance(mContext);
	}

	/**
	 * Release all internal objects
	 */
	private void release() {
		if ((mDbManager != null) && mDbManager.isOpen()) {
			mDbManager.close();
		}
		mDbManager = null;
		iDBOpenCount = 0;
	}

	private int iDBOpenCount = 0;

	private void checkOpenDatabase() {
		if (mDbManager == null) {
			mDbManager = DatabaseManager.getWritableInstance(mContext);
		}
		if (!mDbManager.isOpen()) {
			mDbManager.open();
		}
		iDBOpenCount++;
	}

	private void checkCloseDatabase() {
		iDBOpenCount--;
		if ((iDBOpenCount == 0) && mDbManager.isOpen()) {
			mDbManager.close();
		}
	}

	private void initInternal() {
		this.checkOpenDatabase();
		this.getListSMSPatent();
		this.getListSMSScheduler();
		this.getListWeekOneSMS();
		this.getListSendSMS();
		this.checkCloseDatabase();
	}

	public String getCurrentTimeStamp() {
		SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		String strDate = sdfDate.format(now);
		return strDate;
	}

	// sms patent
	public boolean addSMSPatent(Sms s) {
		if (s == null) {
			Log.e(LOG_TAG, "try to pass null list matches");
			return false;
		}

		synchronized (this) {
			checkOpenDatabase();
			mDbManager.addSMSPatent(s);
			checkCloseDatabase();
		}

		return true;
	}

	public List<Sms> getListSMSPatent() {
		List<Sms> sms = new ArrayList<Sms>();
		checkOpenDatabase();
		synchronized (this) {
			sms = mDbManager.getListSMSPatent();
		}
		checkCloseDatabase();
		return sms;
	}

	public void removeListSMSPatent() {

		synchronized (this) {
			checkOpenDatabase();
			mDbManager.removeAllSMSPatent();
			checkCloseDatabase();
		}

	}

	public boolean removeListSMS(Sms s) {
		boolean result = false;
		synchronized (this) {
			checkOpenDatabase();
			if (mDbManager.removeSMSPatent(s))
				result = true;
			else {
				result = false;
			}
			checkCloseDatabase();
		}
		return result;
	}

	// sms scheduler
	public boolean addSMSScheduler(SchedulerSMSItem s) {
		if (s == null) {
			Log.e(LOG_TAG, "try to pass null list matches");
			return false;
		}

		synchronized (this) {
			checkOpenDatabase();
			mDbManager.addSMSScheduler(s);
			checkCloseDatabase();
		}

		return true;
	}

	public List<SchedulerSMSItem> getListSMSScheduler() {
		List<SchedulerSMSItem> sms = new ArrayList<SchedulerSMSItem>();
		checkOpenDatabase();
		synchronized (this) {
			sms = mDbManager.getListSMSScheduler();
		}
		checkCloseDatabase();
		return sms;
	}

	public void removeListSMSScheduler() {

		synchronized (this) {
			checkOpenDatabase();
			mDbManager.removeAllSMSScheduler();
			checkCloseDatabase();
		}

	}

	public boolean removeListSMS(SchedulerSMSItem s) {
		boolean result = false;
		synchronized (this) {
			checkOpenDatabase();
			if (mDbManager.removeSMSScheduler(s))
				result = true;
			else {
				result = false;
			}
			checkCloseDatabase();
		}
		return result;
	}

	public boolean updateStatusScheduler(SchedulerSMSItem s) {
		boolean result = false;
		synchronized (this) {
			checkOpenDatabase();
			if (mDbManager.updateStatusScheduler(s))
				result = true;
			else {
				result = false;
			}
			checkCloseDatabase();
		}
		return result;
	}

	public boolean updateScheduler(SchedulerSMSItem s) {
		boolean result = false;
		synchronized (this) {
			checkOpenDatabase();
			if (mDbManager.updateScheduler(s))
				result = true;
			else {
				result = false;
			}
			checkCloseDatabase();
		}
		return result;
	}

	// sms patent
	public boolean addWeekOneSMS(OneSMS s) {
		if (s == null) {
			Log.e(LOG_TAG, "try to pass null list matches");
			return false;
		}

		synchronized (this) {
			checkOpenDatabase();
			mDbManager.addWeekOneSMS(s);
			checkCloseDatabase();
		}

		return true;
	}

	public List<OneSMS> getListWeekOneSMS() {
		List<OneSMS> sms = new ArrayList<OneSMS>();
		checkOpenDatabase();
		synchronized (this) {
			sms = mDbManager.getListWeekOneSMS();
		}
		checkCloseDatabase();
		return sms;
	}

	public boolean updateOneSMS(OneSMS s) {
		boolean result = false;
		synchronized (this) {
			checkOpenDatabase();
			if (mDbManager.updateOneSMS(s))
				result = true;
			else {
				result = false;
			}
			checkCloseDatabase();
		}
		return result;
	}

	public int getCurrentPage(int total) {
		int result;
		result = Math.round(((float) total / 10));
		if (result == 0)
			result = 1;
		return result;
	}

	public int getCurrentPageEx(int total) {
		int result = total % 10;

		if (result < 5 && result > 0)
			result = total / 10 + 1;
		else
			result = Math.round(((float) total / 10));

		if (result == 0)
			result = 1;

		return result;
	}

	public List<Integer> convertStringToList(String string) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		if ((string != null) && (string.trim().length() > 0)) {
			String[] med = string.trim().split(",");

			if (med.length == 0 && string.length() > 0) {
				result.add(Integer.parseInt(string));
			}

			for (int i = 0; i < med.length; i++) {
				try {
					result.add(Integer.parseInt(med[i]));
				} catch (Throwable t) {
				}
			}
		}
		return result;
	}

	public String convertListToString(List<Integer> list) {
		StringBuilder sb = new StringBuilder();
		for (Integer id : list) {
			if (sb.length() == 0) {
				sb.append("" + id);
			} else {
				sb.append("," + id);
			}
		}
		return sb.toString();
	}

	public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
		int width = bm.getWidth();
		int height = bm.getHeight();
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// create a matrix for the manipulation
		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight);
		// recreate the new Bitmap
		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
				matrix, false);

		return resizedBitmap;

	}

	// sms patent
	public boolean addSendSMS(SendSms s) {
		if (s == null) {
			Log.e(LOG_TAG, "try to pass null list matches");
			return false;
		}

		synchronized (this) {
			checkOpenDatabase();
			mDbManager.addSendSMS(s);
			checkCloseDatabase();
		}

		return true;
	}

	public List<SendSms> getListSendSMS() {
		List<SendSms> sms = new ArrayList<SendSms>();
		checkOpenDatabase();
		synchronized (this) {
			sms = mDbManager.getListSendSMS();
		}
		checkCloseDatabase();
		return sms;
	}

	public void removeListSendSMS() {

		synchronized (this) {
			checkOpenDatabase();
			mDbManager.removeAllSendSMS();
			checkCloseDatabase();
		}

	}

	public boolean removeListSendSMS(SendSms s) {
		boolean result = false;
		synchronized (this) {
			checkOpenDatabase();
			if (mDbManager.removeSendSMS(s))
				result = true;
			else {
				result = false;
			}
			checkCloseDatabase();
		}
		return result;
	}

}
